package anim;

import dae.Input;
import dae.Sampler;
import dae.Source;

import java.util.ArrayList;
import java.util.List;

public class ParseAnimUtil
{
    public static final String NODE_PREFIX = "node-";
    public static final String SAMPLER_POSTFIX = "_matrix-sampler";
    public static final String INPUT_TYPE = "INPUT";
    public static final String OUTPUT_TYPE = "OUTPUT";
    public static final String INTERPOLATION_TYPE = "INTERPOLATION";

    public static List <Interpolation> parseInterpolationArray(Source source)
    {
        List <Interpolation> interpolationList = new ArrayList <>();
        String[] ss = source.Name_array.value.split(" ");
        if (ss.length != source.Name_array.count)
        {
            AnimationClips.logger.info("{}:interpolation array data length  {}not equal size{}", source.id, ss.length, source.float_array.count);
            return interpolationList;
        }
        for (String s : ss)
        {
            if (s.equals(Interpolation.LINEAR.name()))
            {
                interpolationList.add(Interpolation.LINEAR);
            }
        }
        return interpolationList;
    }

    public static List <Matrix3D> parseMatrix3dArray(Source source)
    {
        List <Matrix3D> matrixList = new ArrayList <>();
        String[] ss = source.float_array.value.split(" ");
        if (ss.length != source.float_array.count)
        {
            AnimationClips.logger.info("{}:matrix array data length  {}not equal size{}", source.id, ss.length, source.float_array.count);
            return matrixList;
        }
        for (int i = 0; i < ss.length; i += 16)
        {
            Matrix3D matrix3D = new Matrix3D();
            for (int j = 0; j < 16; j++)
            {
                matrix3D.update(j / 4, j % 4, Float.parseFloat(ss[i + j]));
            }
            matrixList.add(matrix3D);
        }
        return matrixList;

    }

    public static List <Float> parseFloatArray(Source source)
    {
        List <Float> floatList = new ArrayList <>();
        String[] ss = source.float_array.value.split(" ");
        if (ss.length != source.float_array.count)
        {
            AnimationClips.logger.info("{}:float array data length  {}not equal size{}", source.id, ss.length, source.float_array.count);
            return floatList;
        }
        for (String s : ss)
        {
            float va = Float.parseFloat(s);
            floatList.add(va);
        }
        return floatList;
    }

    public static Input getSpecialTypeFromInput(Sampler sampler, String inputType)
    {
        for (Input input1 : sampler.input)
        {
            if (input1.semantic.equals(inputType))
            {
                return input1;
            }
        }
        return null;
    }

    public static void initBoneAnimFromAnimClipData(Input inputInfo, Input outputInfo, Input interpolationInfo, AnimFragments clip, Source inputSource, Source outputSource, Source interpolationSource)
    {
        List <Float> inputList = null;
        if (inputInfo != null)
        {
            inputList = parseFloatArray(inputSource);
        }
        List <Matrix3D> outputList = null;
        if (outputInfo != null)
        {
            outputList = parseMatrix3dArray(outputSource);
        }
        List <Interpolation> interpolationList = null;
        if (interpolationInfo != null)
        {
            interpolationList = parseInterpolationArray(interpolationSource);
        }
        clip.inputs = inputList;
        clip.outputs = outputList;
        clip.interpolations = interpolationList;
    }
}
